package br.com.androstok;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Pesquisar extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pesquisar);

		Button btPesquisar = (Button) findViewById(R.id.btPesquisar);
		btPesquisar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View botao) {

				if (botao.getId() == R.id.btPesquisar) {

					String pesquisa = pesquisa();

					System.out.println("Resposta: " + pesquisa);

					if (!(pesquisa.equals("false"))) {
						limpar();
						Toast.makeText(Pesquisar.this,
								"Código válido!!!", Toast.LENGTH_LONG)
								.show();
						startActivity(new Intent(Pesquisar.this,
								Relatorio.class));

					} else {
						limpar();
						Toast.makeText(Pesquisar.this, "Código inválido!!!",
								Toast.LENGTH_LONG).show();

					}

				}

			}
		});
	}

	public String pesquisa() {
		EditText codigo = (EditText) findViewById(R.id.etCodigo);
		String msg = "";
		int cod = 0;
		try {
			System.out.println("Teste: " + codigo.getText().toString());
			cod = Integer.parseInt(codigo.getText().toString());
			if (cod <= 0) {

				msg = "false";

			} else {
				msg = "true";
			}

		} catch (NullPointerException npe) {
			msg = "false";
		}

		return msg;
	}

	public void voltar(View botao) {
		limpar();
		startActivity(new Intent(Pesquisar.this, Login.class));
	}

	public void limpar() {

		EditText etCodigo = (EditText) findViewById(R.id.etCodigo);
		etCodigo.setText("");
		etCodigo.requestFocus();

	}

}
